"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库atan函数
Case Name   : 非兼容b库下执行atan函数，合理报错
Create At   : 2023.07.05
Owner       : z@wan005
Description :
    1、创建数据库
    2、连接兼容a库、pg库、c库执行atan函数
    3、清理环境
Expect      :
    1、成功
    2、报错
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class CompatibilityTest15(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_name = "db_atan_0015"
        self.db_compatibility = {f'{self.db_name}_a': 'a',
                                 f'{self.db_name}_pg': 'pg',
                                 f'{self.db_name}_c': 'c'}
        self.expect = 'ERROR:  function atan(integer, integer) does not exist'

    def test_atan(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:创建数据库 expect:成功-----'
        self.log.info(text)
        for dbname, compatibility in self.db_compatibility.items():
            create_cmd = f"drop database if exists {dbname};" \
                         f"create database {dbname} dbcompatibility " \
                         f"'{compatibility}';"
            msg = self.sh_primary.execut_db_sql(create_cmd)
            self.log.info(msg)
        self.assertIn('CREATE DATABASE', msg, '执行失败:' + text)

        text = '-----step2:连接兼容a库、pg库、c库执行atan函数 expect:报错-----'
        self.log.info(text)
        sql_cmd = "select atan(2,-2);"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=f'{self.db_name}_a')
        self.log.info(msg)
        self.assertIn(self.expect, msg, '执行失败:' + text)
        self.log.info(text)
        sql_cmd = "select atan(2,-2);"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=f'{self.db_name}_pg')
        self.log.info(msg)
        self.assertIn(self.expect, msg, '执行失败:' + text)
        self.log.info(text)
        sql_cmd = "select atan(2,-2);"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=f'{self.db_name}_c')
        self.log.info(msg)
        self.assertIn(self.expect, msg, '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        drop_msg = []
        for dbname in self.db_compatibility.keys():
            sql_cmd = f"drop database if exists {dbname};"
            msg = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
            self.log.info(msg)
            drop_msg.append(msg)
        self.assertEqual(3, drop_msg.count('DROP DATABASE'), '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
